package com.decent.ejfadmin.report.service.impl;

import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.report.dao.CollectCardUserReportDao;
import com.decent.ejfadmin.report.response.CollectCardUserReportDto;
import com.decent.ejfadmin.report.service.CollectCardUserReportService;
import com.decent.ejfadmin.common.bean.SupplyCard;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weicx
 * @date 2018/08/01
 */
@Service
@Slf4j
public class CollectCardUserReportServiceImpl implements CollectCardUserReportService {
    @Autowired
    private CollectCardUserReportDao collectCardUserReportDao;

    /**
     * 收卡会员统计
     *
     * @param pageUnit    分页组件
     * @param startTime   查询开始时间
     * @param endTime     查询结束时间
     * @param userAccount 会员编号/名称
     * @param area        发行地区
     * @return Page
     */
    @Override
    public Page collectCardUserReportPage(Page pageUnit, String startTime, String endTime, String userAccount, String area) {
        SupplyCard card = new SupplyCard();
        card.setArea(area);
        int total = collectCardUserReportDao.collectCardUserReportCount(userAccount, startTime, endTime, card);
        pageUnit.setTotal(total);
        if (total == 0) {
            pageUnit.setResultList(new ArrayList());
        } else {
            pageUnit.setResultList(collectCardUserReportDao.collectCardUserReportPage(pageUnit, userAccount, startTime, endTime, card));
        }
        return pageUnit;
    }

    /**
     * 收卡会员统计导出
     *
     * @param startTime   查询开始时间
     * @param endTime     查询结束时间
     * @param userAccount 会员编号/名称
     * @param area        发行地区
     * @return Page
     */
    @Override
    public void downLoad(String startTime, String endTime, String userAccount, String area, HttpServletResponse response) {
        SupplyCard card = new SupplyCard();
        card.setArea(area);
        int total = collectCardUserReportDao.collectCardUserReportCount(userAccount, startTime, endTime, card);
        BufferedWriter bufferedWriter = null;
        try {
            ServletOutputStream servletOutputStream = response.getOutputStream();
            response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".csv");
            Page page = new Page();
            page.setSize(5000);
            page.setTotal(total);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(servletOutputStream, "GBK"));
            bufferedWriter.write("会员编号,会员名称,上传日期,销卡地区,收卡数,移动收卡面值,移动结算面值," +
                    "联通收卡面值,联通结算面值,电信收卡面值,电信结算面值,收卡总面值,已结算面值");
            bufferedWriter.newLine();
            while (page.getPage() <= page.getMaxPage()) {
                List<CollectCardUserReportDto> list = collectCardUserReportDao.collectCardUserReportPage(page, userAccount, startTime, endTime, card);
                int size = list.size();
                StringBuffer data = new StringBuffer(size * 100);
                for (int i = 0; i < size; i++) {
                    data.append(list.get(i));
                }
                bufferedWriter.write(StringUtils.replace(data.toString(), "null", ""));
                page.setPage(page.getPage() + 1);
            }
            bufferedWriter.flush();
        } catch (Exception e) {
            log.warn("收卡会员统计导出出错", e);
        } finally {
            IOUtils.closeQuietly(bufferedWriter);
        }
    }
}
